#include "isotropic_toplayer.h"
#include <QDebug>
#include <QTimer>

IsotropicTopLayer::IsotropicTopLayer(QString layerName,
				     double hBottom,
				     double hTop,
				     QObject *parent) :
    IsotropicAtmosphere(layerName, hBottom, hTop, parent)
{
}

void IsotropicTopLayer::recvSolar(double intensity, double zenith, double azimuth)
{
	qDebug() << "topLayer::recvSolar().";
	AtmosLayer::recvSolar(intensity, zenith, azimuth);

	//top layer need not wait  "emitLightDown" signal
	//it starts caculate when it recieve solar light
	QTimer::singleShot(10, this, SLOT(downInResponse()));
//	downInResponse();
}

#include <QtConcurrentRun>
void IsotropicTopLayer::downInResponse()
{
	m_loopDownTime++;

	qDebug() << m_layerName << "calculating downward incident light's response for the " << m_loopDownTime << "times.";

	//no downward scatter light at the most top layer
	//only downward solar incident light
	solarScatCaculate();

	//caculate downward irridiance, sendout getDownIrridiance() signal
	//must caculate here before emitLight()(downLight will be clear)
	downIrradianceCaculate();

	emitLight();

	return;

}

void IsotropicTopLayer::recvScatInUp(QByteArray lightData)
{
    qDebug() << m_layerName << "recieved upward scatter light";

    QDataStream strm(lightData);

    for(int i = 0; i < N_PI/2; i++){
	    for(int j = 0; j < 2*N_PI; j++){
		    strm >> m_lightInUp[i][j][0] >>
			    m_lightInUp[i][j][1] >>
			    m_lightInUp[i][j][2];
	    }

    }

    upInResponse();
//    QTimer::singleShot(10, this, SLOT(upInResponse()));

    //Change caculate direction to DOWN when top layer recieved
    //upward scatter light, this change direction of emitLight().
    qDebug() << m_layerName << "changing caculating direction.\n------------------------------------------------------\n";
    m_currentDir = DOWN;
    m_loopDownTime++; //toplayer spectial, m_loopDownTime++ here

    emitLight(); //save UPWARD result when emit light DOWN over

}
